<template lang="html">
  <div class="order-form form-contain">
    <!-- 订单管理 -->
    <!-- 面包屑导航 -->
    <el-radio-group v-model="mode3" @change="changeNavigation">
      <el-radio-button :label="0">全部订单</el-radio-button>
      <el-radio-button :label="2">待上课</el-radio-button>
      <el-radio-button :label="3">已完成</el-radio-button>
    </el-radio-group>
    <a class="order-export" :href="'https://jyapi.epanshi.com/api/exportOrderExcel?limit=10&trade_state=' + mode3 + '&page=' + currentPage">导出本页订单数据</a>
    <!-- 搜索 -->
    <div class="block">
      <span class="demonstration">下单时间</span>
      <el-date-picker
        v-model="value1"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        unlink-panels
        value-format="timestamp"
        style="margin-left:20px">
      </el-date-picker>
      <!-- <el-date-picker
        v-model="value1"
        type="date"
        placeholder="起始时间"
        format="yyyy 年 MM 月 dd 日"
        value-format="timestamp">
      </el-date-picker>
      <span>—</span>
      <el-date-picker
        v-model="value2"
        type="date"
        placeholder="结束时间"
        format="yyyy 年 MM 月 dd 日"
        value-format="timestamp">
      </el-date-picker> -->
      <span class='buy'>买家</span>
      <el-input
        v-model="input1"
        clearable
        class='my-btn'>
      </el-input>
      <span class='buy'>订单编号</span>
      <el-input
        v-model="input2"
        clearable
        class='my-btn'>
      </el-input>
      <el-button icon="el-icon-search" class='search' type='primary' @click="searchShop">搜索</el-button>
    </div>
    <div class="order-table">
      <el-table
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          label="商品"
          width="500">
          <template slot-scope="scope">
            <img :src="scope.row.order_img" alt="" srcset="" class="table-img">
            <span class="table-msg">{{scope.row.goods_name}}</span>
            <span class="table-msg">订单编号 : {{scope.row.order_id}}</span>
            <span class="table-msg">下单时间 : {{scope.row.create_at}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="goods_unit_price"
          label="单价"
          width="180">
        </el-table-column>
        <el-table-column
          prop="goods_num"
          label="数量">
        </el-table-column>
        <el-table-column
          label="买家&电话">
          <template slot-scope="scope1">
            <span class="table-msg1">{{scope1.row.contact_name}}</span>
            <span class="table-msg1">{{scope1.row.contact_mobile}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="total_pay"
          label="订单金额">
        </el-table-column>
        <el-table-column
          prop="trade_state"
          label="交易状态">
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope2">
            <span class="table-msg2" v-if='scope2.row.trade_state !== "已完成"' @click='cancelOrder(scope2.row.order_id)'>取消订单</span>
            <span
            class="table-msg2"
            style="cursor:pointer;color:#3f76b5"
            @click="showmsgbox(scope2.row.order_id, scope2.$index)"
            v-if='scope2.row.trade_state === "待上课"'>| 已完成</span>
            <span v-else style="color:#101010">{{scope2.row.invoice}}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class='pagination-box'>
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-size="10"
        layout="prev, pager, next, total, jumper"
        :total="totalPage">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {getOrderList, deleteOrderList, searchOrderList, OrderLischanget} from '@/api/order'
export default {
  data () {
    return {
      mode3: 0,
      value1: '', // 起始时间
      input1: null, // 买家名
      input2: null, // 订单号
      tableData: [],
      currentPage4: 1,
      totalPage: null,
      currentPage: 1
    }
  },
  methods: {
    changeNavigation () {
      this.load()
    },
    handleCurrentChange (val) {
      this.currentPage = val
      this.load()
    },
    completeOrder () {
      this.$confirm('此操作将完成订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '订单已完成!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '操作失败'
        })
      })
    },
    // 取消订单
    cancelOrder (orderId) {
      this.$confirm('此操作将取消订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {
          order_id: orderId + ''
        }
        deleteOrderList(params).then(res => {
          if (res.code === 1) {
            this.load()
            this.$message({
              type: 'success',
              message: '订单已取消!'
            })
          }
        })
      }).catch(() => {
      })
    },
    showmsgbox (rowId, rowindex) {
      this.$confirm('确认已开发票?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {
          order_id: rowId
        }
        OrderLischanget(params).then(res => {
          this.$message({
            type: 'success',
            message: '发票已开!'
          })
          this.load()
        }).catch(err => {
          console.log(err)
        })
      }).catch(() => {
      })
    },
    load () {
      let params = {
        order_id: this.input2,
        wx_nickname: this.input1,
        start_time: this.value1[0] ? this.value1[0] / 1000 : '',
        end_time: this.value1[1] ? this.value1[1] / 1000 : '',
        trade_state: this.mode3,
        page: this.currentPage
      }
      getOrderList(params).then(res => {
        console.log(res)
        for (let i = 0; i < res.data.dataList.length; i++) {
          if (res.data.dataList[i].trade_state === 1) {
            res.data.dataList[i].trade_state = '待付款'
          } else if (res.data.dataList[i].trade_state === 2) {
            res.data.dataList[i].trade_state = '待上课'
          } else {
            res.data.dataList[i].trade_state = '已完成'
          }
        }
        this.totalPage = res.data.total
        this.tableData = res.data.dataList
      }).catch(err => {
        console.log(err)
      })
    },
    // 搜索订单
    searchShop () {
      let params = {
        order_id: this.input2,
        wx_nickname: this.input1,
        start_time: this.value1[0] ? this.value1[0] / 1000 : '',
        end_time: this.value1[1] ? this.value1[1] / 1000 : '',
        trade_state: this.mode3,
        page: this.currentPage
      }
      searchOrderList(params).then(res => {
        for (let i = 0; i < res.data.dataList.length; i++) {
          if (res.data.dataList[i].trade_state === 1) {
            res.data.dataList[i].trade_state = '待付款'
          } else if (res.data.dataList[i].trade_state === 2) {
            res.data.dataList[i].trade_state = '待上课'
          } else {
            res.data.dataList[i].trade_state = '已完成'
          }
        }
        this.totalPage = res.data.total
        this.tableData = res.data.dataList
      }).catch(err => {
        console.log(err)
      })
    }
  },
  mounted () {
    this.load()
  }
}
</script>

<style lang="less">
.order-form{
  font-size: 14px;
  .order-export{
    float: right;
    color: #3f76b5;
    cursor: pointer;
  }
  .block{
    margin-top: 20px;
    margin-bottom: 30px
  }
  .el-radio-group{
    display: block;
    margin-top: 30px;
  }
  .demonstration{
    width: 100%;
    margin-left: 30px
  }
  .buy{
    margin-left: 30px
  }
  .my-btn{
    width: 10%;
    margin-left: 10px
  }
  .search{
    margin-left: 30px
  }
  .el-date-editor.el-input{
    width: 15%;
    margin-left: 10px;
  }
  .order-table{
    min-height: 600px;
    .table-img{
      height: 71px;
      width: 60px;
      float: left;
    }
    .table-msg{
      display: block;
      line-height: 30px;
      margin-left: 80px;
    }
    .table-msg1{
      display: block;
      line-height: 30px;
    }
    .table-msg2{
      line-height: 30px;
      color:#3f76b5;
      cursor: pointer;
    }
  }
  .pagination-box{
    width: 90%;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
